<?php

namespace App\model;

use support\Model;

class GuildMember extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'guild_member';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = true;

    protected $dateFormat = 'U';

    // 定义一个访问器，用于格式化输出
    public function getCreatedAtAttribute($value)
    {
        return date('Y-m-d H:i:s', strtotime($value));
    }

    public function getGuildByUuidRoleID($uuid, $role_id)
    {
        $guild_id = self::query()->where('role_id', $role_id)->where('uuid', $uuid)->value('guild_id');
        return intval($guild_id);
    }

    public function getGuildFightCap($guild)
    {
        $guildMembers = $this->where('guild_id', $guild)->get();
        $guildMembers = $guildMembers ? $guildMembers->toArray() : [];
        $total_fight_cap = 0;
        foreach ($guildMembers as $item) {
            $where_cond = [];
            $where_cond['role_id'] = $item['role_id'];
            $where_cond['uuid'] = $item['uuid'];
            $role_info = Role::query()->where($where_cond)->first();
            $role_info = $role_info ? $role_info->toArray() : [];
            $total_fight_cap = $total_fight_cap + $role_info['fight_cap'] ?? 0;
        }
        return $total_fight_cap;
    }

}
